package com.stedu.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.stedu.bean.Admin;
import com.stedu.exception.SteduException;
import com.stedu.mapper.AdminMapper;
import com.stedu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws SteduException {
        //调用dao的方法
        Admin admin= adminMapper.selelecByUsername(username);
        if(admin == null){
            throw new SteduException("用户名错误，请确认后重新输入");
        }
        //对用户输入密码加密
        String md5pwd = SecureUtil.md5(SecureUtil.md5(password));
        if (!md5pwd.equals(admin.getPassword())){
            throw new SteduException("密码错误，请确认后重新输入");
        }
        return admin;
    }

    @Override
    public boolean insert(Admin admin) throws SteduException {
        if(adminMapper.sellectByUsername(admin.getUsername()) !=null){
            throw new SteduException("该用户名已经存在，无法重复添加");
        }else{
            if(adminMapper.sellectByOid(admin.getOid())!=null){
                throw new SteduException("该用户已有账密，无法重复添加");
            }
            if(adminMapper.sellectByPid(admin.getPid())!=null){
                throw new SteduException("该用户已有账密，无法重复添加");
            }
        }
        return adminMapper.insert(admin) == 1;
    }


    @Override
    public boolean updateByOid(Admin admin) throws SteduException {
        if (adminMapper.sellectByUsername(admin.getUsername()) != null&& adminMapper.sellectByOid((adminMapper.sellectByUsername(admin.getUsername())).getOid())==adminMapper.sellectByOid(admin.getOid())){
            throw new SteduException("该用户名存在，无法修改");
        }
        return adminMapper.updateByOid(admin) == 1;
    }
    @Override
    public boolean updateByPid(Admin admin) throws SteduException {
        if (adminMapper.sellectByUsername(admin.getUsername()) != null){
            throw new SteduException("该用户名存在，无法修改");
        }
        return adminMapper.updateByPid(admin) == 1;
    }

    @Override
    public boolean deleteByOid(Integer oid) throws SteduException {
        return adminMapper.deleteByOid(oid) == 1;
    }
    @Override
    public boolean deleteByPid(Integer pid) throws SteduException {
        return adminMapper.deleteByPid(pid) == 1;
    }

    @Override
    public List<Admin> sellectAll() {
        return adminMapper.sellectAll();
    }

    @Override
    public Admin sellectByOid(Integer oid) {
        return adminMapper.sellectByOid(oid);
    }

    @Override
    public Admin sellectByPid(Integer pid) {
        return adminMapper.sellectByOid(pid);
    }

    @Override
    public List<Admin> sellectByRoot(Integer root) {
        return adminMapper.sellectByRoot(root);
    }

    @Override
    public List<Map<String, Object>> selectTop5DnameAndCount() {
        return adminMapper.selectTop5DnameAndCount();
    }
    @Override
    public List<Map<String, Object>> selectTop5PnameAndCount() {
        return adminMapper.selectTop5PnameAndCount();
    }

}
